﻿@using System.Data;
@using GatewayServer.Common;
@using UtilsBase;
@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {

        if ("@(ViewBag.order)" == "upd") {
            getEdit("@(ViewBag.mv_guid)");
        }
        else {

            getVariableList();
        }

        getEditRelationList();


    });


    function getEdit(guid) {
        $.post("getVariableRelation", { "flag": Math.random(), guid: guid }, function (result) {
            
            var myobj = (result.data);
            $("#mv_limit").val(myobj.mv_limit);
            $("#hid_mv_v_guid").val(myobj.mv_v_guid);
            getVariableList();
        });
    }
    function getVariableList() {
        $("#mv_v_guid").empty();
        $("#mv_v_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableList", { "flag": Math.random() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#mv_v_guid").prepend("<option value='" + myobj[i].v_guid + "'>" + myobj[i].v_name + "</option>")
            }
            for (var i = 0; i < document.getElementById("mv_v_guid").options.length; i++) {
                if (document.getElementById("mv_v_guid").options[i].value == $("#hid_mv_v_guid").val()) {
                    document.getElementById("mv_v_guid").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("mv_v_guid").options[i].selected = false;

                }
            }
        });
    }
    function getEditRelationList() {
        $.post("getEditRelationList", { "flag": Math.random(), modelguid: $("#mv_model_guid").val(), type: "follow" }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >序号</td >";
            htmlstr = htmlstr + "<td >变量</td >";
            htmlstr = htmlstr + "<td >名称</td >";
            htmlstr = htmlstr + "<td >量程</td >";
            htmlstr = htmlstr + "<td >操作</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_follow").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + rowcount + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].key + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].mv_limit + "</td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:delRelation('" + pagedata[i].mv_guid + "')\">删除</a> </td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:editRelation('" + pagedata[i].mv_guid + "')\">修改</a> </td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_follow").innerHTML = htmlstr;
            }
        });
    }
    function saveAll() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveAllVariable", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
              //  window.location.href = "list";
                getEditRelationList();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function delRelation(guid) {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("delRelation?guid=" + guid, data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                getEditRelationList();
            
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function editRelation(guid) {
    
        window.location.href = "?order=upd&guid=" + guid + "&modelguid=" + $("#mv_model_guid").val();

    }
    
</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label {
        width: 150px;
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>变量配置</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">
                        <input type="hidden" name="mv_model_guid" id="mv_model_guid" value="@ViewBag.mv_model_guid" />
                        <input type="hidden" name="mv_guid" id="mv_guid" value="@ViewBag.mv_guid" />
                        
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>变量：</label>
                            <div class="kv-item-content">
                                <select id="mv_v_guid" name="mv_v_guid" style="width:130px;">
                                    <option value="">请选择</option>
                                </select>
                                <input type="hidden" name="hid_mv_v_guid" id="hid_mv_v_guid" value="@ViewBag.mv_guid" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>量程范围：</label>
                            <div class="kv-item-content">
                                <input type="text" name="mv_limit" id="mv_limit" placeholder="量程范围" /> [如：0-100000]
                            </div>
                        </div>
                       
                        <div><hr />

                            <div>
                                <table id="pagelist_follow" class="plainItemTable"></table>
                            </div>
                        
                        </div>


                        <br /><br />

                        <div style="margin:0 auto;width:150px;">

                            <input type="button" id="save" name="save" value="保存变量" onclick="javascript:saveAll()" />
                            &nbsp;&nbsp; <input type="button" id="but_back1" name="but_back1" value="返回" onclick="javascript: window.location.href = 'list'" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

